package com.dd.admin.controller.log;

import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.common.dto.system.log.SysJobLogQueryDTO;
import com.dd.common.dto.system.log.SysLoginLogQueryDTO;
import com.dd.common.dto.system.log.SysOperateLogQueryDTO;
import com.dd.common.entity.SysJobLog;
import com.dd.common.entity.SysLoginLog;
import com.dd.common.entity.SysOperateLog;
import com.dd.common.pojo.dto.PageDTO;
import com.dd.common.service.SysJobLogService;
import com.dd.common.service.SysLoginLogService;
import com.dd.common.service.SysOperateLogService;
import com.dd.common.utils.AjaxResult;
import com.dd.common.utils.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author cheng
 */
@SaIgnore
@Validated
@Slf4j
@Tag(name = "日志管理")
@RestController("/log")
public class LogController {
    @Resource
    private SysOperateLogService sysOperateLogService;
    @Resource
    private SysJobLogService sysJobLogService;
    @Resource
    private SysLoginLogService sysLoginLogService;

    @Operation(summary = "登录日志")
    @GetMapping("/login/page")
    public AjaxResult<?> loginLog(@Validated PageDTO pageDTO, @Validated SysLoginLogQueryDTO dto) {
        Page<SysLoginLog> page = Page.of(pageDTO.getCurrent(), pageDTO.getSize());
        LambdaQueryWrapper<SysLoginLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(dto.getUsername()), SysLoginLog::getUsername, dto.getUsername())
                .like(StringUtils.hasText(dto.getIp()), SysLoginLog::getIp, dto.getIp())
                .like(StringUtils.hasText(dto.getLocation()), SysLoginLog::getLocation, dto.getLocation())
                .like(StringUtils.hasText(dto.getOs()), SysLoginLog::getOs, dto.getOs())
                .like(StringUtils.hasText(dto.getBrowser()), SysLoginLog::getBrowser, dto.getBrowser())
                .eq(dto.getStatus() != null, SysLoginLog::getStatus, dto.getStatus())
                .ge(dto.getCreatedAtStart() != null, SysLoginLog::getCreateTime, dto.getCreatedAtStart())
                .le(dto.getCreatedAtEnd() != null, SysLoginLog::getCreateTime, dto.getCreatedAtEnd())
                .orderByAsc(SysLoginLog::getCreateTime);
        IPage<SysLoginLog> result = sysLoginLogService.page(page, queryWrapper);
        return AjaxResult.success(PageResult.iPageHandle(result.getTotal(), result.getCurrent(), result.getSize(), result.getRecords()));
    }

    @Operation(summary = "定时任务日志")
    @GetMapping("/job/page")
    public AjaxResult<?> jobLog(@Validated PageDTO pageDTO, @Validated SysJobLogQueryDTO dto) {
        Page<SysJobLog> page = Page.of(pageDTO.getCurrent(), pageDTO.getSize());
        LambdaQueryWrapper<SysJobLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(dto.getJobName()), SysJobLog::getJobName, dto.getJobName())
                .like(StringUtils.hasText(dto.getJobGroup()), SysJobLog::getJobGroup, dto.getJobGroup())
                .like(StringUtils.hasText(dto.getInvokeTarget()), SysJobLog::getInvokeTarget, dto.getInvokeTarget())
                .eq(dto.getStatus() != null, SysJobLog::getStatus, dto.getStatus())
                .ge(dto.getStartTime() != null, SysJobLog::getStartTime, dto.getStartTime())
                .le(dto.getEndTime() != null, SysJobLog::getEndTime, dto.getEndTime())
                .orderByAsc(SysJobLog::getId);
        IPage<SysJobLog> result = sysJobLogService.page(page, queryWrapper);
        return AjaxResult.success(PageResult.iPageHandle(result.getTotal(), result.getCurrent(), result.getSize(), result.getRecords()));
    }

    @Operation(summary = "系统操作日志")
    @GetMapping("/operate/page")
    public AjaxResult<?> operateLog(@Validated PageDTO pageDTO, @Validated SysOperateLogQueryDTO dto) {
        Page<SysOperateLog> page = Page.of(pageDTO.getCurrent(), pageDTO.getSize());
        LambdaQueryWrapper<SysOperateLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(dto.getTitle()), SysOperateLog::getTitle, dto.getTitle())
                .eq(dto.getBusinessType() != null, SysOperateLog::getBusinessType, dto.getBusinessType())
                .eq(dto.getOperatorType() != null, SysOperateLog::getOperatorType, dto.getOperatorType())
                .like(StringUtils.hasText(dto.getMethod()), SysOperateLog::getMethod, dto.getMethod())
                .eq(dto.getRequestMethod() != null, SysOperateLog::getRequestMethod, dto.getRequestMethod())
                .eq(dto.getUserId() != null, SysOperateLog::getUserId, dto.getUserId())
                .like(StringUtils.hasText(dto.getUserName()), SysOperateLog::getUserName, dto.getUserName())
                .like(StringUtils.hasText(dto.getUrl()), SysOperateLog::getUrl, dto.getUrl())
                .like(StringUtils.hasText(dto.getIp()), SysOperateLog::getIp, dto.getIp())
                .eq(dto.getStatus() != null, SysOperateLog::getStatus, dto.getStatus())
                .ge(dto.getCreatedAtStart() != null, SysOperateLog::getCreateTime, dto.getCreatedAtStart())
                .le(dto.getCreatedAtEnd() != null, SysOperateLog::getCreateTime, dto.getCreatedAtEnd())
                .orderByAsc(SysOperateLog::getId);

        IPage<SysOperateLog> result = sysOperateLogService.page(page, queryWrapper);
        return AjaxResult.success(PageResult.iPageHandle(result.getTotal(), result.getCurrent(), result.getSize(), result.getRecords()));
    }

}
